package com.gpnu7_412.yanzhi.service.impl;

import com.gpnu7_412.yanzhi.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**
 * 发送邮件
 * @author ZZH
 */
@Service
public class EmailServiceImpl implements EmailService {
    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String fromEmail;

    @Override
    public void sendEmail(String toEmail, String subject, String text) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        //设置发件邮箱
        simpleMailMessage.setFrom(fromEmail);
        // 收件人邮箱
        simpleMailMessage.setTo(toEmail);
        // 主题标题
        simpleMailMessage.setSubject(subject);
        // 信息内容
        simpleMailMessage.setText(text);
        // 执行发送
        try {
            mailSender.send(simpleMailMessage);
        } catch (Exception e) {
            throw new RuntimeException("验证码发送失败，请重试或联系管理员");
        }
    }
}
